package com.hsy.crm.web.service.impl;

import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.Customer;
import com.hsy.crm.web.domain.entity.CustomerType;
import com.hsy.crm.web.domain.ex.ServiceException;
import com.hsy.crm.web.domain.po.customerType.CustomerTypeSelectPo;
import com.hsy.crm.web.domain.vo.customerType.CustomerTypeSelectVo;
import com.hsy.crm.web.mapper.CustomerMapper;
import com.hsy.crm.web.mapper.CustomerTypeMapper;
import com.hsy.crm.web.service.base.BaseService;
import com.hsy.crm.web.utils.DateUtils;
import com.hsy.crm.web.utils.UuidUtils;
import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.Customer;
import com.hsy.crm.web.domain.entity.CustomerType;
import com.hsy.crm.web.domain.ex.ServiceException;
import com.hsy.crm.web.domain.po.customerType.CustomerTypeSelectPo;
import com.hsy.crm.web.domain.vo.customerType.CustomerTypeSelectVo;
import com.hsy.crm.web.mapper.CustomerTypeMapper;
import com.hsy.crm.web.utils.DateUtils;
import com.hsy.crm.web.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CustomerTypeService extends BaseService<CustomerType> {

    @Autowired
    CustomerTypeMapper customerTypeMapper;

    @Autowired
    CustomerMapper customerMapper;

    public Unified<List<CustomerTypeSelectPo>> select(CustomerTypeSelectVo customerTypeSelectVo) {
        Unified<List<CustomerTypeSelectPo>> unified = new Unified<>();
        unified.setData(customerTypeMapper.find(customerTypeSelectVo));
        return unified;
    }

    public Unified<Integer> insertCustomerType(CustomerType customerType) {
        Unified<Integer> unified = new Unified<>();

        repeat_check_insert(customerType);

        customerType.setId(UuidUtils.getUuid());
        customerType.setCreateTime(DateUtils.millis());
        customerType.setCreateUserId(customerType.getLoginUserId());
        if(customerType.getParentId()==null){
            customerType.setParentId(-1L);
        }

        unified.setData(customerTypeMapper.insertCustomerType(customerType));
        return unified;
    }

    public Unified<Integer> deleteCustomerType(CustomerType customerType) {
        Unified<Integer> unified = new Unified<>();

        int result = check_delete(customerType);
        if( result != App.Result.SUCCESS ){
            unified.setResult(result);
            return unified;
        }
        this.delete(customerType);
        return unified;
    }

    public Unified<Integer> updateCustomerType(CustomerType customerType) {
        checkUpdate(customerType);
        return Unified.newInstance(this.update(customerType));
    }

    private void checkUpdate(CustomerType customerType){
        Integer count = customerTypeMapper.updateCheck(customerType);
        if( count != null ){
            throw new ServiceException(App.BussError.LEVEL_NAME_REPEAT.getCode());
        }
    }

    private void repeat_check_insert(CustomerType customerType){
        //check name
        CustomerType checkType = new CustomerType();
        checkType.setParentId(customerType.getParentId());
        checkType.setName(customerType.getName());
        checkType.setCustomerId(customerType.getCustomerId());
        checkType.setCompanyId(customerType.getLoginCompanyId());
        int count = customerTypeMapper.selectCount(checkType);
        if( count > 0 ){
            throw new ServiceException(App.BussError.LEVEL_NAME_REPEAT.getCode());
        }
    }

    private int check_delete(CustomerType customerType){

        // has children
        CustomerType checkType = new CustomerType();
        checkType.setParentId(customerType.getId());
        checkType.setCustomerId(customerType.getCustomerId());
        int count = customerTypeMapper.selectCount(checkType);
        if( count > 0 ){
            return App.BussError.DELETE_CHILD.getCode();
        }

        // has product
        Customer entity = new Customer();
        entity.setCustomerTypeId(customerType.getId());
        entity.setCustomerId(customerType.getCustomerId());
        count = customerMapper.selectCount(entity);
        if( count > 0 ){
            return App.BussError.CUS_TYPE_DELETE_CUS.getCode();
        }

        return App.Result.SUCCESS;
    }

}
